﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.ExtNet;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Demo.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;
namespace Sharp.WebBooter.Areas.Demo.Controllers
{
    [AutomaticTemplate]
    public class DataController : Controller
    {
        public ActionResult Index(string containerId)
        {
            if (containerId.IsNullOrEmpty())
            {
                return View(new GroupIndexModel());
            }
            return new PartialViewResult
            {
                Model = new GroupIndexModel(),
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }
        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(ExtRequestParameters parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var pageInfo = proxy.Channel.GetGroupPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo.Data, pageInfo.TotalRecords);
            }
        }


        public ActionResult Index2(string containerId)
        {
            if (containerId.IsNullOrEmpty())
            {
                return View(new GroupIndexModel()
                {
                    ListControlId = "ListControlId2",
                    StoreUrlAction = nameof(GetPaged2)
                });
            }
            return new PartialViewResult
            {
                Model = new GroupIndexModel()
                {
                    StoreUrlAction = nameof(GetPaged2)
                },
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }
        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged2(ExtRequestParameters parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var pageInfo = proxy.Channel.GetGroupPaged(parameters.ToPagingFeature());
                pageInfo.Data.Clear();
                return this.Store(pageInfo.Data, pageInfo.TotalRecords);
            }
        }
    }
}